<script setup lang="ts">
import { request } from '@/api'
import { flattenAreaData, type Area } from '@/tool/areatext'
import b64ToBlob from 'b64-to-blob'
import { showToast, type ActionSheetAction } from 'vant'
import { onMounted, ref } from 'vue'

const UserList = ref({} as HDMUser)

onMounted(() => {
  UserList.value = mk.queryUser()
  console.log(UserList.value)
})
const onCancel = () => {
  showProfession.value = false
  showToast('取消')
}
const show = ref(false)
const selectItem = async (action: ActionSheetAction, index: number) => {
  show.value = false
  if (index === 1) {
    const b64Str = await mk.pickerPhoto()
    getfile(b64Str)
  } else {
    const b64Str = await mk.pickerCamera()
    getfile(b64Str)
  }
}

const getfile = async (Str: string) => {
  // 3. 转换为2进制blob
  const file = b64ToBlob(Str, 'image/jpeg')

  // 4. fd组装数据
  const fd = new FormData()
  fd.append('file', file, Date.now() + '.jpeg')

  // 5 .
  const res = await request.post('member/profile/avatar', fd)
  UserList.value.avatar = res.data.result.avatar
}

const columns = ref<Area[]>([])

const str = mk.getAreaColumns()

columns.value = JSON.parse(str)
const areaData = flattenAreaData(JSON.parse(str))

const showArea = ref(false)
const showPopup = ref(false)
const currentDate = ref(['2021', '01', '01'])

interface IPickerParams {
  selectedValues: string[]
  selectedOptions: {
    text: string
    value: string
  }[]
  selectedIndexes: number[]
}
const onSelectedLocation = (data: IPickerParams) => {
  showArea.value = false
  const pName = areaData[data.selectedValues[0]]
  const cName = areaData[data.selectedValues[1]]
  const aName = areaData[data.selectedValues[2]]
  UserList.value.fullLocation = `${pName} ${cName} ${aName}`
}

interface birthdayBobj {
  selectedValues: ['2021', '01', '01']
  selectedOptions: [
    {
      text: '2021'
      value: '2021'
    },
    {
      text: '01'
      value: '01'
    },
    {
      text: '01'
      value: '01'
    },
  ]
  selectedIndexes: [121, 0, 0]
}

const onSelectedBirth = (data: birthdayBobj) => {
  const birthday = data.selectedValues
  const year = birthday[0]
  const yue = birthday[1]
  const day = birthday[2]
  UserList.value.birthday = year + '-' + yue + '-' + day
  showPopup.value = false
}
const showProfession = ref(false)

interface Profession {
  selectedValues: ['教师']
  selectedOptions: [
    {
      text: '教师'
      value: '教师'
    },
  ]
  selectedIndexes: [2]
}

const onCancelProfession = (data: Profession) => {
  console.log(data.selectedValues)
  const Prof = data.selectedValues
  UserList.value.profession = Prof[0]
  showProfession.value = false
}
const columnsProfession = [
  { text: '软件工程师', value: '软件工程师' },
  { text: '医生', value: '医生' },
  { text: '教师', value: '教师' },
  { text: '律师', value: '律师' },
  { text: '会计师', value: '会计师' },
  { text: '销售经理', value: '销售经理' },
  { text: '市场营销专员', value: '市场营销专员' },
  { text: '建筑师', value: '建筑师' },
  { text: '护士', value: '护士' },
  { text: '机械工程师', value: '机械工程师' },
]

const onSubmit = async () => {
  const res = await request.put('/member/profile', UserList.value)
  mk.updateUser(res.data.result)
  showToast('修改成功')
}
</script>

<template>
  <div class="profile-edit-page">
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image round width="100" height="100" class="avatar-img" :src="UserList.avatar">
      </van-image>
      <div class="avatar-btn" @click="show = true">
        <span>修改头像</span>
      </div>
    </div>
    <div>
      <van-action-sheet
        @select="selectItem"
        v-model:show="show"
        :actions="[{ name: '拍摄' }, { name: '相册' }]"
        cancel-text="取消"
      />
    </div>

    <!-- 弹出层  日期-->
    <van-popup position="bottom" v-model:show="showPopup">
      <van-date-picker
        @confirm="onSelectedBirth"
        v-model="currentDate"
        title="选择日期"
        :min-date="new Date(1900, 0, 1)"
        :max-date="new Date()"
      />
    </van-popup>

    <!-- 弹出层 地区 -->
    <van-popup position="bottom" v-model:show="showArea">
      <van-picker
        @cancel="showArea = false"
        @confirm="onSelectedLocation"
        :columns-field-names="{ text: 'name', value: 'code', children: 'areaList' }"
        title="地址"
        :columns="columns"
      />
    </van-popup>

    <!-- 职业 -->
    <van-popup position="bottom" v-model:show="showProfession">
      <van-picker
        title="职业"
        :columns="columnsProfession"
        @confirm="onCancelProfession"
        @cancel="onCancel"
      />
    </van-popup>

    <!-- 中间表单部分 -->
    <van-cell-group>
      <van-fieldv v-model="UserList.account" label="账号" readonly></van-fieldv>
      <van-field v-model="UserList.nickname" label="昵称" placeholder="请输入昵称"></van-field>
      <van-cell title="性别" class="gender">
        <van-radio-group :icon-size="16" v-model="UserList.gender">
          <van-radio name="男">男</van-radio>
          <van-radio name="女">女</van-radio>
          <van-radio name="未知">未知</van-radio>
        </van-radio-group>
      </van-cell>
      <van-field
        @click="showPopup = true"
        label="生日"
        readonly
        placeholder="请选择日期"
        v-model="UserList.birthday"
      ></van-field>
      <van-field
        @click="showArea = true"
        label="所在地"
        readonly
        placeholder="请选择所在地"
        v-model="UserList.fullLocation"
      ></van-field>
      <van-field
        @click="showProfession = true"
        label="职业"
        readonly
        placeholder="请选择职业"
        v-model="UserList.profession"
      ></van-field>
    </van-cell-group>

    <div class="submit">
      <van-button round block type="primary" @click="onSubmit"> 保存资料 </van-button>
    </div>
  </div>
</template>

<style lang="css" scoped>
.avatar {
  padding: 30px;
  text-align: center;
  background-color: var(--mk-white);
}

.avatar-img {
  box-shadow: 0 0 5px #ccc;
}

.avatar-btn {
  color: var(--mk-linear_end);
  margin-top: 10px;
}

.gender ::v-deep(.van-cell__title) {
  width: 100px;
  flex: none;
}

.van-radio-group {
  display: flex;
  justify-content: space-between;
  height: 36px;
  width: 180px;
}

.submit {
  padding: 16px;
}
</style>
